<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb>
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索栏和添加用户区域 -->
      <el-row :gutter="20">
        <el-col :span="9">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear='getUserList'>
            <el-button slot="append" icon="el-icon-search" @click="getUserList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="3">
          <el-button type="primary" @click="addUser">添加用户</el-button>
        </el-col>
      </el-row>
      <!-- 用户列表区域 -->
        <el-table :data="userList" stripe border>
          <el-table-column type="index"></el-table-column>
          <el-table-column prop="username" label="用户名"></el-table-column>
          <el-table-column prop="role_name" label="角色"></el-table-column>
          <el-table-column prop="mobile" label="电话"></el-table-column>
          <el-table-column prop="email" label="邮箱"></el-table-column>
          <el-table-column prop="mg_state" label="状态">
            <template slot-scope="scope">
              <el-switch v-model="scope.row.mg_state" active-color="#22b362" inactive-color="#e43737" @change="stateChange(scope.row)"></el-switch>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button type="primary" icon="el-icon-edit" size="mini" @click="editUser(scope.row.id)"></el-button>
              <el-button type="danger" icon="el-icon-delete"  size="mini" @click="deleteUser(scope.row.id)"></el-button>
              <el-tooltip effect="dark" content="角色分配" placement="top-start" :enterable='false'>
                <el-button size="mini" type="warning" icon="el-icon-setting" @click='setRole(scope.row)'></el-button>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页区域 -->
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
        </el-pagination>
    </el-card>
    <!-- 添加用户对话框 -->
    <addUser ref="addUser"></addUser>
    <!-- 修改用户对话框 -->
    <editUser ref="editUser"></editUser>
    <!-- 分配角色对话框 -->
    <setRole ref="setRole"></setRole>
  </div>
</template>
<script>
import addUser from '../user/dialog/addUser'
import editUser from '../user/dialog/editUser'
import setRole from '../user/dialog/setRole'
export default {
  data: function () {
    return {
      // 用户列表查询的参数对象
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 2
      },
      // 用户列表数据
      userList: [],
      // 总条数
      total: 0
    }
  },
  components: {
    addUser,
    editUser,
    setRole
  },
  created: function () {
    this.getUserList()
  },
  methods: {
    // 获取用户列表数据
    getUserList: async function () {
      const res = await this.$http.get('users', { params: this.queryInfo })
      if (res.meta.status !== 200) {
        return this.$message.warning('用户数据列表获取失败！')
      }
      this.userList = res.data.users
      this.total = res.data.total
    },
    // 监听状态改变
    stateChange: async function (row) {
      const res = await this.$http.put(`users/${row.id}/state/${row.mg_state}`)
      if (res.meta.status !== 200) {
        return this.$message.info('状态设置失败')
      }
      this.$message.success('设置状态成功')
      this.getUserList()
    },
    // 监听pagesize 改变的事件
    handleSizeChange: function (pagesize) {
      this.queryInfo.pagesize = pagesize
      this.getUserList()
    },
    // 监听当前pagenum改变的事件
    handleCurrentChange: function (pagenum) {
      this.queryInfo.pagenum = pagenum
      this.getUserList()
    },
    // 点击按钮，添加用户
    addUser: function () {
      this.$refs.addUser.dialogVisible = true
    },
    // 点击编辑，修改用户信息
    editUser: function (id) {
      this.$refs.editUser.queryUserInfo(id)
      this.$refs.editUser.dialogVisible = true
    },
    // 点击按钮，删除用户
    deleteUser: async function (id) {
      const confirmResult = await this.$confirm('确定删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'

      }).catch(err => err)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      const res = await this.$http.delete(`users/${id}`)
      if (res.meta.status !== 200) {
        return this.$message.info('删除用户信息失败')
      }
      this.$message.success('删除用户信息成功')
      this.getUserList()
    },
    // 点击按钮，分配角色
    setRole: function (id) {
      this.$refs.setRole.getRoleList(id)
      this.$refs.setRole.dialogVisible = true
    }
  }
}
</script>
<style lang="less" scoped>
.el-card {
  padding: 10px 30px;
  .el-row .el-col:nth-child(1) .el-button {
    color: #81baf5;
    font-size: 14px;
    padding: 10px 18px;
  }
}
.el-table {
  width: 100%;
  margin: 20px 0;
}
</style>
